package com.tencent.qf.bascic.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencent.qf.bascic.api.SCRMService;
import com.tencent.qf.bascic.api.dto.SendMsgDTO;
import com.tencent.qf.bascic.api.dto.SendMsgRequest;
import com.tencent.qf.common.pojo.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.tencent.qf.common.gateway.scrm.SCRMClient;
import com.tencent.qf.common.gateway.scrm.SCRMApiPath;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: p_haojiawu@tencent.com
 * @date: 2023-02-08 09:57
 **/

@Service
@Slf4j
public class SCRMServiceImpl implements SCRMService {

    @Resource
    private SCRMClient scrmClient;
    @Override
    public HttpResult<List<SendMsgDTO>> sendMsg(SendMsgRequest request) {
        String result = scrmClient.post(request, SCRMApiPath.V1_PUSHMSG_SENDMSG);
        try{
            JSONObject parse = (JSONObject) JSONObject.parse(result);
            if(parse.getInteger("code") == 1){
                List<SendMsgDTO> data = JSONArray.parseArray(parse.getString("data"), SendMsgDTO.class);
                return HttpResult.success(data);
            }else{
                return HttpResult.error(500, parse.getString("msg"));
            }
        }catch (Exception e){
            String substring = result.substring(result.indexOf("{"), result.length() - 1);
            JSONObject parse = (JSONObject) JSONObject.parse(substring);
            return HttpResult.error(500, parse.getString("msg"));
        }
    }
}
